//
//  LoginViewController.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/16.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "FindPasswordViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "UserInfo.h"
#import "FileHandle.h"

#define UserSuccessLogin [NSString stringWithFormat:@"userSuccessLogin"]


@interface LoginViewController ()

@end

@implementation LoginViewController

// 找回密码
- (IBAction)findPasswordBT:(id)sender
{
    FindPasswordViewController *findPasswordVC = [[FindPasswordViewController alloc]init];
    [self.navigationController pushViewController:findPasswordVC animated:YES];
}


// 登录
- (IBAction)loginBT:(id)sender
{
    [AVUser logInWithUsernameInBackground:_UesrName.text password:_passWord.text block:^(AVUser *user, NSError *error) {
        
        if (user != nil) {
            
            UIAlertView*aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [aler show];
            [UserInfo shareUserInfo].isLogin = YES;
            // 登陆成功  发布通知
            [[NSNotificationCenter defaultCenter]postNotificationName:UserSuccessLogin object:nil];
            // 登陆之后，
            [self.navigationController popViewControllerAnimated:YES];
            // 如果 plist 存在 就调用plist 不存在 就创建plist
            [[FileHandle fileHandle]foundUserName:_UesrName.text];
            // userIfo 代理方法
            [[UserInfo shareUserInfo].delegate userInfo:[UserInfo shareUserInfo] select:YES];
            return ;
        }else{
            
            UIAlertView*aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码或用户名不正确" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [aler show];
            
        }
        
    }];
    
}

// 注册
- (IBAction)registBT:(id)sender
{
    RegistViewController *reginstVC = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:reginstVC animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.title = @"登录页面";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_chbackbtn"] style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
